package com.uzai.console.web.cms;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.cms.base.CmsBaseInfoDto;
import com.uzai.console.dto.locallive.tlbtoken.TlbTokenQuery;
import com.uzai.console.dto.sysdomain.SysDomainQuery;
import com.uzai.console.dto.tb.tbtoken.TbTokenQuery;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.entity.SysDomain;
import com.uzai.console.entity.TlbToken;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.mapper.SysDomainMapper;
import com.uzai.console.service.cms.CmsBaseService;
import com.uzai.console.service.configplan.ConfigPlanService;
import com.uzai.console.vo.base.SelectIdVo;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.cms.base.CmsBaseInfoVo;
import com.uzai.console.vo.cms.base.CmsBasePlatFormSelectVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * CMS小程序|基础设置
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "基础设置",tags = "CMS小程序|基础设置")
@ApiSort(7)
@RestController
public class CmsBaseController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(CmsBaseController.class);

    @Autowired
    private CmsBaseService cmsBaseService;
    @Autowired
    private SysDomainMapper sysDomainMapper;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private ConfigPlanService configPlanService;

    @ApiOperation(value = "查询CMS基础设置信息", notes = "查询CMS基础设置信息", response = CmsBaseInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/cmsBase/findCmsBaseInfo")
    public Object findCmsBaseInfo(){
        return cmsBaseService.findCmsBaseInfo(getLoginInfo().getMerId());
    }

    @SystemLogType(model = SystemModelEnum.CmsBase, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "保存CMS基础设置信息", notes = "保存CMS基础设置信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/cmsBase/updateCmsBase")
    public Object updateCmsBase(@RequestBody CmsBaseInfoDto cmsBaseInfoDto){
        cmsBaseService.updateCmsBase(cmsBaseInfoDto);
        return true;
    }

    @ApiOperation(value = "查询商城域名下拉列表框", notes = "查询商城域名下拉列表框", response = SelectIdVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/cmsBase/findSysDomainSelectList")
    public Object findSysDomainSelectList(){
        //新建返回页面对象
        List<SelectIdVo> sysDomainVoList = new ArrayList<SelectIdVo>();
        //官方域名
        SelectIdVo selectIdVo_system = new SelectIdVo();
        selectIdVo_system.setKey(0L);
        selectIdVo_system.setValue("官方域名");
        sysDomainVoList.add(selectIdVo_system);

        //快站域名
        SysDomainQuery sysDomainQuery_kuaizhan = new SysDomainQuery();
        sysDomainQuery_kuaizhan.setMerId(getLoginInfo().getMerId());
        sysDomainQuery_kuaizhan.setStatus(1);
        sysDomainQuery_kuaizhan.setType(1);//快站域名
        List<SysDomain> sysDomainList_kuaizhan =  sysDomainMapper.findByList(sysDomainQuery_kuaizhan);
        if(sysDomainList_kuaizhan != null && sysDomainList_kuaizhan.size() > 0){
            for (SysDomain sysDomain : sysDomainList_kuaizhan) {
                SelectIdVo  SelectIdVo = new SelectIdVo();
                SelectIdVo.setKey(sysDomain.getId());
                SelectIdVo.setValue(sysDomain.getSiteDomain() + "（快站域名）");
                sysDomainVoList.add(SelectIdVo);
            }
        }

        //自有域名
        SysDomainQuery sysDomainQuery_private = new SysDomainQuery();
        sysDomainQuery_private.setMerId(getLoginInfo().getMerId());
        sysDomainQuery_private.setStatus(1);
        sysDomainQuery_private.setType(2);//快站域名
        List<SysDomain> sysDomainList_private =  sysDomainMapper.findByList(sysDomainQuery_private);
        if(sysDomainList_private != null && sysDomainList_private.size() > 0){
            for (SysDomain sysDomain : sysDomainList_private) {
                SelectIdVo  SelectIdVo = new SelectIdVo();
                SelectIdVo.setKey(sysDomain.getId());
                SelectIdVo.setValue(sysDomain.getSiteDomain() + "（自有域名）");
                sysDomainVoList.add(SelectIdVo);
            }
        }
        return sysDomainVoList;
    }

    @ApiOperation(value = "查询系统授权过的功能模块列表", notes = "查询系统授权过的功能模块列表", response = CmsBasePlatFormSelectVo.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/cmsBase/findSysPlatFormSelectList")
    public Object findSysPlatFormSelectList(){
        //新建返回页面对象
        CmsBasePlatFormSelectVo cmsBasePlatFormSelectVo = new CmsBasePlatFormSelectVo();
        //查询当前运营商的设备类型配置
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(getLoginInfo().getMerId(), MerConfigKeyEnum.PRODUCT_SWITCH.getValue());
        if(merBaseConfig != null) { //如果存在，则修改
            String val = merBaseConfig.getVal();
            cmsBasePlatFormSelectVo.setProductKeyList(JSONObject.parseArray(val,String.class));
        }
        return cmsBasePlatFormSelectVo;
    }

    @ApiOperation(value = "查询淘宝联盟下拉列表", notes = "查询淘宝/饿了么联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=5)
    @PostMapping("/cmsBase/findTbTokenSelectList")
    public Object findTbTokenSelectList(@RequestBody TbTokenQuery tbTokenQuery){
        return configPlanService.findTbTokenSelectList(tbTokenQuery);
    }

    @ApiOperation(value = "查询饿了么联盟下拉列表", notes = "查询淘宝/饿了么联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=5)
    @PostMapping("/cmsBase/findElmTokenSelectList")
    public Object findElmTokenSelectList(){
        return configPlanService.findElmTokenSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询唯品会联盟下拉列表", notes = "查询唯品会联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=6)
    @PostMapping("/cmsBase/findVphTokenSelectList")
    public Object findVphTokenSelectList(){
        return configPlanService.findVphTokenSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询京东联盟下拉列表", notes = "查询京东联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=7)
    @PostMapping("/cmsBase/findJdTokenSelectList")
    public Object findJdTokenSelectList(){
        return configPlanService.findJdTokenSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询多多进宝联盟下拉列表", notes = "查询多多进宝联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=8)
    @PostMapping("/cmsBase/findPddTokenSelectList")
    public Object findPddTokenSelectList(){
        return configPlanService.findPddTokenSelectList(getLoginInfo().getMerId());
    }

    @ApiOperation(value = "查询美团联盟(推老板)下拉列表", notes = "查询美团联盟(推老板)下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=9)
    @PostMapping("/cmsBase/findTlbTokenSelectList")
    public Object findTlbMtTokenSelectList(){
        TlbTokenQuery tlbTokenQuery = new TlbTokenQuery();
        tlbTokenQuery.setMerId(getLoginInfo().getMerId());
        tlbTokenQuery.setType("mt");
        return configPlanService.findTlbTokenSelectList(tlbTokenQuery);
    }

}
